package com.tcsl.mylibrary;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.iboxpay.cashbox.minisdk.CashboxProxy;
import com.iboxpay.cashbox.minisdk.PayType;
import com.iboxpay.cashbox.minisdk.SignType;
import com.iboxpay.cashbox.minisdk.callback.IAuthCallback;
import com.iboxpay.cashbox.minisdk.callback.ITradeCallback;
import com.iboxpay.cashbox.minisdk.model.Config;
import com.iboxpay.cashbox.minisdk.model.ErrorMsg;
import com.iboxpay.cashbox.minisdk.model.ParcelableBitmap;
import com.iboxpay.cashbox.minisdk.model.ParcelableMap;
import com.iboxpay.cashbox.minisdk.model.PrintPreference;
import com.tcsl.mylibrary.utils.CryptUtil;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * 描述:
 * <p/>作者：wjx
 * <p/>创建时间: 2017/5/26 16:44
 */
public class PayHeZi extends TcslPay {

    private PrintPreference heziPrintPreference;

    public PayHeZi(Context context) {
        super(context);
    }

    @Override
    protected void pay(final Float money, final String orderNo, String merchantNo) {
        String boxSn = CashboxProxy.getBoxSn(mActivity);
        if (TextUtils.isEmpty(boxSn) || boxSn.equals("false")) {
            mListener.notSupport();
            return;
        }
        heziPrintPreference = new PrintPreference();
        heziPrintPreference.setOrderTitle("天财商龙移动支付");
        Config.config = new Config(BuildConfig.heziAppCode, heziPrintPreference);
        Config.config.setIboxMchtNo(merchantNo);
        try {
            CashboxProxy.getInstance(mContext).initAppInfo(Config.config, new IAuthCallback() {
                @Override
                public void onAuthSuccess() {
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            payHeZi(orderNo+"_"+timeStamp,money);
                        }
                    });
                }

                @Override
                public void onAuthFail(final ErrorMsg msg) {
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mListener.payError("签到失败。" + msg.getErrorMsg() + "[" + msg.getErrorCode() + "]");
                        }
                    });
                }
            });
        } catch (Throwable e) {
            e.printStackTrace();
            mListener.payError("签到异常。\n错误信息:" + e.toString());
        }
    }

    /**
     * 支付
     *
     * @param orderNo
     * @param money
     */
    private void payHeZi(String orderNo, Float money) {
        //注意：盒子POS支付的金额必须乘100，所传的String类型为100，不能为100.00或100.0
        try {
            final String amount = String.valueOf(Math.round((Double.valueOf(money) * 100)));
            String transactionId = timeStamp ;
            ParcelableMap additionalMap = new ParcelableMap();
            additionalMap.put(ParcelableMap.TRANSACTION_ID, transactionId);
            additionalMap.put(ParcelableMap.RESV, "wwxxnn");

            additionalMap.put(ParcelableMap.ORDER_TIME, timeStamp);
            String sign = CryptUtil.getDefaultSign(Config.config, amount, orderNo, null, BuildConfig.heziMD5Key,
                    additionalMap.getMap());
            //发起交易
            CashboxProxy.getInstance(mContext).startTrading(PayType.TYPE_CARD, amount,
                    orderNo, transactionId,
                    SignType.TYPE_MD5, sign, additionalMap, new ITradeCallback() {
                        @Override
                        public void onTradeSuccess(ParcelableMap map) {
                            mActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mListener.paySuccess("盒子支付支付成功", "");
                                }
                            });
                        }

                        //设置了不显示盒子签购单时回调此方法。
                        @Override
                        public void onTradeSuccessWithSign(ParcelableMap map,
                                                           ParcelableBitmap signBitmap) {
                        }

                        @Override
                        public void onTradeFail(final ErrorMsg msg) {
                            mActivity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Log.e("Hezi", "----盒子POS交易失败----");
                                    mListener.payError(msg.getErrorMsg() + "[" + msg.getErrorCode() + "]");
                                }
                            });
                        }
                    });
        } catch (Throwable e) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            mListener.payError("结算通道异常，目前无法结算，请拍照保留。\n错误信息1 " + e.toString() + "\n错误信息2 " + sw.toString());
        }
    }


}
